
const UA = navigator.userAgent ?? "";
const searchParams = new URLSearchParams(window.location.search);

export function isDevContext() {
    return +window.location.port === 8081;
    // return +window.location.port === 8023;
}

export function isIFrameContext() {
    return self !== top;
}

export function isNamedContext(name) {
    return searchParams.has(name);
}

export function isKioskContext() {
    const host = document.referrer.split("/")[2] ?? "";
    const override = /dailymail/i.test(host);
    return isNamedContext("kiosk") && !override;
}

export function isNonKioskIFrameContext() {
    return isIFrameContext() && !isKioskContext();
}

export function isDebugContext() {
    return isNamedContext("debug");
}

export function isAppContext() {
    return isNamedContext("app");
}

export function siteInstance() {
    return window.location.hostname.match(/(.*)\.nullschool\.net$/)?.[1] ?? "unknown";
}

export function siteVersion() {
    return document.querySelector("html").dataset.version ?? "unknown";
}

/**
 * @returns {boolean} true if agent is probably firefox. Don't really care if this is accurate.
 */
export function isFirefox() {
    return (/firefox/i).test(UA);
}

export function isSafari() {
    return (/safari/i).test(UA);
}

/**
 * @returns {boolean} true if agent is probably iOS. Don't really care if this is accurate.
 */
export function isIOS() {
    // https://stackoverflow.com/questions/56578799/tell-ipados-from-macos-on-the-web
    return (/ipad|iphone|ipod/i).test(UA) || (/macintosh/i).test(UA) && navigator.maxTouchPoints > 2;
}

export function isAndroid() {
    return /android/i.test(UA);
}

/**
 * @returns {boolean} true if agent is probably a mobile device. Don't really care if this is accurate.
 */
export function isMobile() {
    return (/android|blackberry|iemobile|ipad|iphone|ipod|opera mini|webos/i).test(UA);
}

export function isWebView() {
    // https://developer.chrome.com/multidevice/user-agent
    return isAndroid() && /\swv/.test(UA) || isIOS() && !isSafari();
}

/** @returns {boolean} true if the device's primary input method can hover over elements */
export function isHoverSupported() {
    return window.matchMedia("(hover: hover)").matches;
}

// UNDONE: test for fullscreen / app mode:
//         https://www.quora.com/How-do-I-know-if-a-user-is-accessing-a-PWA-app-from-a-browser-or-a-home-screen
